package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.GrandChildExamData;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildExamDataDaoImpl extends TempDataObjectDao<GrandChildExamData> {

    public static String getGrandChildExamDataSql = "select * from `t_c_grand_child_exam_data` where globalId >= ?";

    public Map<Long, GrandChildExamData> getGrandChildExamDataMap(long minGlobalId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, GrandChildExamData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getGrandChildExamDataSql);
            int i = 1;
            call.setLong(i++, minGlobalId);
            rs = call.executeQuery();
            while (rs.next()) {
                GrandChildExamData parse = parse(rs);
                map.put(parse.getGlobalId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateGrandChildExamDataSql = "update `t_c_grand_child_exam_data` set groupId=?,serverList=?,openTime=?,finishTime=?,openTimeParam=? where globalId=?";

    @Override
    protected boolean updateData(GrandChildExamData grandChildExamData) {
        grandChildExamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, grandChildExamData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(grandChildExamData.getServerList())));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildExamData.getOpenTime()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildExamData.getFinishTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, grandChildExamData.getOpenTimeParam()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildExamData.getGlobalId()));
        if (getDb().executeNoneQuery(updateGrandChildExamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGrandChildExamDataSql = "insert into `t_c_grand_child_exam_data`(globalId,groupId,serverList,openTime,finishTime,openTimeParam) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(GrandChildExamData grandChildExamData) {
        grandChildExamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, grandChildExamData.getGlobalId()));
        para.put(i++, new DbParameter(Types.INTEGER, grandChildExamData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(grandChildExamData.getServerList())));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildExamData.getOpenTime()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildExamData.getFinishTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, grandChildExamData.getOpenTimeParam()));
        if (getDb().executeNoneQuery(insertGrandChildExamDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GrandChildExamData parseData(ResultSet rs) throws SQLException {
        GrandChildExamData grandChildExamData = new GrandChildExamData();
        grandChildExamData.setGlobalId(rs.getLong("globalId"));
        grandChildExamData.setGroupId(rs.getInt("groupId"));
        grandChildExamData.setServerList(JSON.parseObject(rs.getString("serverList"), new TypeReference<ConcurrentHashSet<Long>>() {
        }));
        grandChildExamData.setOpenTime(rs.getLong("openTime"));
        grandChildExamData.setFinishTime(rs.getLong("finishTime"));
        grandChildExamData.setOpenTimeParam(rs.getString("openTimeParam"));
        return grandChildExamData;
    }
}
